package movie.controller;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import movie.entity.Cinema;
import movie.entity.CinemaName;
import movie.entity.Film;
import movie.entity.FilmDate;
import movie.entity.Hall;
import movie.entity.Schedule;
import movie.entity.Seat;
import movie.service.CinemaNameService;
import movie.service.CinemaScheduleFindAllService;
import movie.service.FilmFindAllService;
import movie.service.HallService;
import movie.service.SeatService;

@Controller
public class UserSelectMovieController {
	
	@Autowired
	private FilmFindAllService filmFindAllService;
	@Autowired
	private CinemaScheduleFindAllService cinemaScheduleFindAllService;
	@Autowired
	private HallService hallService;
	@Autowired
	private CinemaNameService cinemaNameService;
	

	@Autowired
	private SeatService seatService;

	//选电影--->go to 选影院post方法
	@RequestMapping(value = "/user/select/movie", method = RequestMethod.GET)
	public String get(Model model) {
		List<Film> films = filmFindAllService.findAll();
		model.addAttribute("films", films);
		return "user/selectmovie";
	}
	//选影院--->go to 选电影post方法
	@RequestMapping(value = "/user/select/cinema", method = RequestMethod.GET)
	public String get(Model model,HttpServletRequest request) {
		HttpSession session = request.getSession(false);
		int movieId = (int) session.getAttribute("movieId");
		
		List<Cinema> cinemas=cinemaScheduleFindAllService.find(movieId);
		
		model.addAttribute("cinemas", cinemas);
		
		return "user/selectcinema";
	}
	//选影院--->go to 排片方法
	@RequestMapping(value = "/user/select/cinema", method = RequestMethod.POST)
	public String post(int movieId,Model model,HttpServletRequest request) {
		Film film= filmFindAllService.findMovieId(movieId);
		model.addAttribute("movieId", movieId);
		HttpSession session = request.getSession();
		List<Cinema> cinemas=cinemaScheduleFindAllService.Userfind(movieId);	
		model.addAttribute("cinemas", cinemas);
		session.setAttribute("movieId", movieId);
		session.setAttribute("consumer_id",1);
		List<String> cinemanames=cinemaNameService.findAllCinemaName();
		model.addAttribute("film", film);
		model.addAttribute("cinemanames", cinemanames);
		return "user/selectcinema";
	}
	
	//排片--->go to 选座方法
	@RequestMapping(value = "/paipian/{mid}&{cid}", method = RequestMethod.GET)
	public String getSchedule(@PathVariable("mid") int movieId,@PathVariable("cid") int cinemaId,Model model,HttpServletRequest request) {
		HttpSession session = request.getSession(false);
		//电影ID
		//int movieId = 1;//(int) session.getAttribute("movieId");
		model.addAttribute("movieId", movieId);
		//影院ID
		//int cinemaId=1;
		model.addAttribute("cinemaId", cinemaId);
		//找影院信息
		CinemaName cinema=cinemaNameService.findCinema(cinemaId);
		model.addAttribute("cinema", cinema);
		//找所有电影
		List<Film> films=cinemaScheduleFindAllService.findAllFilm(cinemaId);
		model.addAttribute("films", films);
		Cinema cinema1=new Cinema();
		cinema1.setMovieId(movieId);
		cinema1.setCinemaId(cinemaId);
		//找movie_id电影的排片
		List<FilmDate> filmDates=cinemaScheduleFindAllService.findAllSchedule(cinema1);
		model.addAttribute("filmDates", filmDates);
		for(FilmDate items:filmDates) {
			System.out.println(items);
		}
		return "Cinema/CinemaSchedule";
	}
	//选座方法
		@RequestMapping(value = "/chooseSeat/{cid}&{mid}&{sid}", method = RequestMethod.GET)
		public String getSeatMassage(@PathVariable("cid") int cinemaId,@PathVariable("mid") int movieId,@PathVariable("sid") int scheduleId,Model model) {
			
			//电影信息
			Film film=filmFindAllService.findMovieId(movieId);
			System.out.println(film);
			model.addAttribute("film", film);
			//找影院信息
			CinemaName cinema=cinemaNameService.findCinema(cinemaId);
			System.out.println(cinema);
			model.addAttribute("cinema", cinema);
			//排片信息
			Cinema schedule= cinemaScheduleFindAllService.findByScheduleId(scheduleId);
			System.out.println(schedule);
			model.addAttribute("schedule", schedule);
			int hallId=schedule.getHallId();
			//返回所有座位信息
			Hall hall=new Hall();
			hall.setCinemaId(cinemaId);
			hall.setHallId(hallId);
			List<Seat> seats=seatService.findAllSeat(hall);	
			System.out.println(seats);
			model.addAttribute("seats", seats);
					
			System.out.println(cinemaId);
			System.out.println(movieId);
			System.out.println(scheduleId);
			return "Cinema/CinemaSeat";
		}
	
	@RequestMapping(value = "/use/find/cinemaschedule", method = RequestMethod.GET)
	public String findcinemaScheduleGet(HttpServletRequest request,String filmBeginTime,Model model,String cinemaName1)
	{
		HttpSession session = request.getSession(false);
		int movieId = (int) session.getAttribute("movieId");
		Cinema cinema=new Cinema();
		cinema.setMovieId(movieId);
		CinemaName cinemaName=new CinemaName();
		cinemaName.setCinemaName(cinemaName1);
		cinema.setCinemaName(cinemaName);
		if(!filmBeginTime.equals("") && !cinemaName1.equals("全部")) {
			List<Cinema> cinemasAll=cinemaScheduleFindAllService.findByMovieIdDateCinemaId(cinema);
			List<Cinema> cinemas=new ArrayList<Cinema>();
			for(Cinema cinema11:cinemasAll)
			{
				SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");		
				cinema11.setFilmBeginTimeString(format1.format(cinema11.getFilmBeginTime()));
				if(cinema11.getFilmBeginTimeString().equals(filmBeginTime))
				{	
					cinemas.add(cinema11);
				}	
			}	
			model.addAttribute("cinemas",cinemas);
		}
		else if(filmBeginTime.equals("") && !cinemaName1.equals("全部"))
		{
			List<Cinema> cinemas=cinemaScheduleFindAllService.findByMovieIdDateCinemaId(cinema);
			model.addAttribute("cinemas",cinemas);
		}
		
		else if(!filmBeginTime.equals("") && cinemaName1.equals("全部"))
		{
			
			List<Cinema> cinemasAll=cinemaScheduleFindAllService.Userfind(movieId);
			List<Cinema> cinemas=new ArrayList<Cinema>();
			for(Cinema cinema11:cinemasAll)
			{
				SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");		
				cinema11.setFilmBeginTimeString(format1.format(cinema11.getFilmBeginTime()));
				if(cinema11.getFilmBeginTimeString().equals(filmBeginTime)) 
				{
//					if(cinemas.size()==1) 
//					{
//						Cinema cinemaChange=new Cinema();
//						cinemas.add(cinemaChange);
//					}
					cinemas.add(cinema11);
				}
			}
			model.addAttribute("cinemas",cinemas);
		}
		else if(filmBeginTime.equals("") && cinemaName1.equals("全部"))
		{
			System.out.println(movieId);
			List<Cinema> cinemas=cinemaScheduleFindAllService.Userfind(movieId);
			model.addAttribute("cinemas",cinemas);
		}
		
		List<String> cinemanames=cinemaNameService.findAllCinemaName();
		model.addAttribute("cinemanames", cinemanames);
			
		
		return "user/selectcinema";
	}
}
